package com.cleaningcloud.cld.service.impl.mini;

import com.cleaningcloud.cld.domain.CldAssetsRecord;
import com.cleaningcloud.cld.domain.CldAssetsRepair;
import com.cleaningcloud.cld.domain.dto.CldAssetsRecordDto;
import com.cleaningcloud.cld.mapper.CldAssetsRecordMapper;
import com.cleaningcloud.cld.mapper.CldAssetsRepairMapper;
import com.cleaningcloud.cld.service.mini.IMiniSuppliesService;
import com.cleaningcloud.common.core.constant.SecurityConstants;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import com.cleaningcloud.erp.api.RemoteWareService;
import com.cleaningcloud.erp.api.domain.ErpWareCommodityNumber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class MiniSuppliesServiceImpl implements IMiniSuppliesService {

    @Autowired
    private CldAssetsRecordMapper assetsRecordMapper;

    @Autowired
    private CldAssetsRepairMapper cldAssetsRepairMapper;

    @Autowired
    private RemoteWareService remoteWareService;

    @Override
    @Transactional
    public int addSuppliesRepair(CldAssetsRecordDto cldAssetsRecord) {
        // 查询当前资产记录明细
        CldAssetsRecord assetsRecord = assetsRecordMapper.selectCldAssetsRecordByAssetsRecordId(cldAssetsRecord.getAssetsRecordId());
        if (assetsRecord != null) {
            assetsRecord.setIsNewUpdate(0L);
            // 设置维修时间
            assetsRecord.setRepairTime(DateUtils.getNowDate());
            assetsRecord.setRepairUserId(SecurityUtils.getUserId());
            assetsRecord.setRepairDesc(cldAssetsRecord.getRepairDesc());
            assetsRecord.setRepairImgs(cldAssetsRecord.getRepairImgs());
            assetsRecord.setRepairMoney(cldAssetsRecord.getRepairMoney());
            assetsRecord.setRepairStatus(cldAssetsRecord.getRepairStatus());
            assetsRecord.setRepairMoney(cldAssetsRecord.getRepairMoney());
            assetsRecord.setWareId(cldAssetsRecord.getWareId() == null ? 10 : cldAssetsRecord.getWareId());
            //  新增资产维修配件明细
            if (cldAssetsRecord.getDetailsList() != null && !cldAssetsRecord.getDetailsList().isEmpty()) {
                cldAssetsRepairMapper.batchDeleteRepairDetailsByRecordId(cldAssetsRecord.getAssetsRecordId());
                cldAssetsRecord.getDetailsList().forEach(item -> {
                    item.setAssetsRecordId(assetsRecord.getAssetsRecordId());
                });
                cldAssetsRepairMapper.batchInsertRepairDetails(cldAssetsRecord.getDetailsList());
                // 更改配件库存
                ErpWareCommodityNumber query = new ErpWareCommodityNumber();
                for (CldAssetsRepair cldAssetsRepair : cldAssetsRecord.getDetailsList()) {
                    // 根据商品id和调出仓库id查询对应库存数据
                    query.setWareId(cldAssetsRepair.getWareId());
                    query.setCommodityId(cldAssetsRepair.getCommodityId());
                    ErpWareCommodityNumber out = remoteWareService.selectByCommodityIdAndWareId(query, SecurityConstants.INNER);
                    // 根据调拨数量进行库存相减
                    if (out.getCommodityNumber() < cldAssetsRepair.getAssetsRepairNumber()) {
                        throw new RuntimeException("当前仓库库存数量小于维修数量");
                    }
                    out.setCommodityNumber(out.getCommodityNumber() - cldAssetsRepair.getAssetsRepairNumber());
                    remoteWareService.updateErpWareCommodityNumber(out, SecurityConstants.INNER);
                }
            }
            assetsRecordMapper.updateCldAssetsRecord(assetsRecord);
            CldAssetsRecord newAssetsRecord = new CldAssetsRecord();
            newAssetsRecord.setRecordStatus(assetsRecord.getLastStatus());
            newAssetsRecord.setProjectId(assetsRecord.getProjectId());
            newAssetsRecord.setPersonId(assetsRecord.getPersonId());
            newAssetsRecord.setAssetsId(assetsRecord.getAssetsId());
            newAssetsRecord.setAssetsIdCard(assetsRecord.getAssetsIdCard());
            newAssetsRecord.setFlag(0L);
            newAssetsRecord.setIsNewUpdate(1L);
            newAssetsRecord.setCreateTime(new Date());
            newAssetsRecord.setLastStatus(assetsRecord.getRecordStatus());
            // 取维修日期
            newAssetsRecord.setRepairTime(DateUtils.getNowDate());
            // 去闲置时间
            newAssetsRecord.setLdleStartTime(assetsRecord.getLdleStartTime());
            assetsRecordMapper.insertCldAssetsRecord(newAssetsRecord);
        }
        return 1;
    }

    @Override
    public int addSuppliesReport(CldAssetsRecordDto cldAssetsRecordDto) {
        // 查询当前资产记录明细
        CldAssetsRecord assetsRecord = assetsRecordMapper.selectCldAssetsRecordByAssetsRecordId(cldAssetsRecordDto.getAssetsRecordId());
        if (assetsRecord != null) {
            assetsRecord.setIsNewUpdate(0L);
            // 闲置中 设置闲置结束时间
            if (assetsRecord.getRecordStatus() == 1) {
                assetsRecord.setLdleEndTime(DateUtils.getNowDate());
            }
            // 投放中 设置投放结束时间
            if (assetsRecord.getRecordStatus() == 2) {
                assetsRecord.setPlacementEndTime(DateUtils.getNowDate());
            }
            assetsRecordMapper.updateCldAssetsRecord(assetsRecord);
            CldAssetsRecord newAssetsRecord = new CldAssetsRecord();
            newAssetsRecord.setAssetsId(assetsRecord.getAssetsId());
            newAssetsRecord.setAssetsIdCard(assetsRecord.getAssetsIdCard());
            newAssetsRecord.setFlag(0L);
            newAssetsRecord.setIsNewUpdate(1L);
            newAssetsRecord.setCreateTime(new Date());
            newAssetsRecord.setOperaUserTime(new Date());
            newAssetsRecord.setLastStatus(assetsRecord.getRecordStatus());
            newAssetsRecord.setOperaUserId(SecurityUtils.getUserId());
            newAssetsRecord.setLdleStartTime(cldAssetsRecordDto.getRecordStatus() == 1 ? DateUtils.getNowDate() : assetsRecord.getLdleStartTime());
            newAssetsRecord.setPlacementStartTime(cldAssetsRecordDto.getRecordStatus() == 2 ? DateUtils.getNowDate() : assetsRecord.getPlacementStartTime());
            newAssetsRecord.setRecordStatus(cldAssetsRecordDto.getRecordStatus());
            newAssetsRecord.setReportUserId(SecurityUtils.getUserId());
            newAssetsRecord.setReportTime(DateUtils.getNowDate());
            newAssetsRecord.setReportDesc(cldAssetsRecordDto.getReportDesc());
            newAssetsRecord.setReportImgs(cldAssetsRecordDto.getReportImgs());
            // 投放中报修
            if (assetsRecord.getRecordStatus() == 2) {
                newAssetsRecord.setProjectId(assetsRecord.getProjectId());
                newAssetsRecord.setPersonId(assetsRecord.getPersonId());
            }
            newAssetsRecord.setRecordStatus(cldAssetsRecordDto.getRecordStatus());
            assetsRecordMapper.insertCldAssetsRecord(newAssetsRecord);
        }
        return 1;
    }
}
